<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" style="width: 80%">
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.goodsName"
                            placeholder="请输入商品名称"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.onSale" placeholder="请选择上架状态" type="select"
                            prop="onSale"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.categoryId" placeholder="请选择分类"
                            :load="categoryCasUrl"
                            :props="{ checkStrictly: true }"
                            type="cas" prop="categoryId"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.brandId" placeholder="请选择品牌"
                            :load="brandRemoteUrl"
                            type="select" prop="brandId"></globalSearchTemplate>
      <!--                    <globalSearchTemplate :config="globalConfig" v-model="listQuery.supplierId" placeholder="供应商"></globalSearchTemplate>-->
      <!--                    <globalSearchTemplate :config="globalConfig" v-model="listQuery.top" placeholder="是否置顶"  type="select" prop="top"></globalSearchTemplate>-->

      <globalSearchTemplate :config="globalConfig" v-model="listQuery.startTime" type="date"
                            placeholder="开始时间"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.endTime" type="date"
                            placeholder="结束时间"></globalSearchTemplate>
      <el-button class="filter-item" v-waves @click="handleSearch" icon="el-icon-search">搜索</el-button>
      <el-button class="filter-item" v-if="goodsManager_btn_add" v-waves @click="handleCreate" type="primary"
                 icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
              highlight-current-row style="width: 100%">
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="编号" prop="serialNumber" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="名称" prop="goodsName" width="250"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="主图" prop="logo" type="img" width="100"></globalTableColumn>
      <!--          <globalTableColumn :data="list" width="80"  :loading.sync="listLoading" :key="key" :config="globalConfig"  align="center" label="置顶" prop="top" type="select" ></globalTableColumn>-->
      <!--                        <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig"  align="center" label="状态" prop="onSale" type="select" ></globalTableColumn>-->
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="分类" prop="categoryId" :load="casDataUrl" type="cas" width="120"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig"
                         align="center" label="品牌" prop="brandId" :load="brandRemoteUrl"
                         type="select" width="120"></globalTableColumn>
      <!--                        <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig"  align="center" label="供应商" prop="supplierId"></globalTableColumn>-->
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig"
                         align="center" label="现金价" prop="retailPrice" width="100"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig"
                         align="center" label="对比价" prop="contrastPrice" width="100"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig"
                         align="center" label="供货价" prop="counterPrice" width="100"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="创建日期" prop="createDate" width="170"></globalTableColumn>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button v-if="goodsManager_btn_upd" size="small" type="success"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="goodsManager_btn_del" size="mini" type="danger"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page"
                     :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <globalFormItem :key="formKey" :config="globalConfig" label="商品名称" v-model="form.goodsName" prop="goodsName"
                        placeholder="请输入商品名称"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="上架状态" v-model="form.onSale" prop="onSale"
                        placeholder="请选择上架状态" type="select"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="置顶设置" v-model="form.top" prop="top"
                        placeholder="请选择是否置顶" type="select"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="商品分类" v-model="form.categoryId" prop="categoryId"
                        placeholder="请选择分类"
                        :props="{ checkStrictly: true }"
                        :load="casDataUrl" type="cas"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="商品品牌" v-model="form.brandId" prop="brandId"
                        placeholder="请选择品牌" :props="{ checkStrictly: true }"
                        :load="brandRemoteUrl" type="select"></globalFormItem>
        <!--                            <globalFormItem :key="formKey" :config="globalConfig" label="供应商"  v-model="form.supplierId"  prop="supplierId" placeholder="请输入供应商"> </globalFormItem>-->
        <globalFormItem :key="formKey" :config="globalConfig" label="现金价" v-model="form.retailPrice" prop="retailPrice"
                        placeholder="请输入现金价"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="对比价" v-model="form.contrastPrice"
                        prop="contrastPrice" placeholder="请输入对比价"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="供货价" v-model="form.counterPrice"
                        prop="counterPrice" placeholder="请输入供货价"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="介绍" v-model="form.introduction" prop="introduction"
                        placeholder="请输入介绍"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="备注" v-model="form.memo" prop="memo"
                        placeholder="请输入备注"></globalFormItem>
        <!--                            <globalFormItem :key="formKey" :config="globalConfig" label="商品关键字，采用逗号间隔"  v-model="form.keywords"  prop="keywords" placeholder="请输入商品关键字，采用逗号间隔"> </globalFormItem>-->
        <!--                            <globalFormItem :key="formKey" :config="globalConfig" label="商品轮播图"  v-model="form.gallery"  prop="gallery" placeholder="请输入商品轮播图"> </globalFormItem>-->


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .filter-item {
    margin-left: 10px;
  }
</style>

<script>
  import { fetchList, getObj, addObj, putObj, delObj, casObj } from '@/api/supplier/goods'
  import { remoteData as brandRemoteData } from '@/api/supplier/brand'
  import { casObj as categoryCasObj } from '@/api/supplier/category'
  import waves from '@/directive/waves/index.js' // 水波纹指令
  import { mapGetters } from 'vuex'
  import { globalNotify } from '@/util/notifyUtils'  //弹窗
  import { getToken } from '@/util/auth'
  import globalSearchTemplate from '@/components/global-el-search-template'
  import globalTableColumn from '@/components/global-el-table-column'
  import globalFormItem from '@/components/global-el-form-item'
  import { resolveUrlPath } from '@/util/util'

  export default {
    name: 'goods',
    components: { globalSearchTemplate, globalTableColumn, globalFormItem },
    directives: {
      waves
    },
    data() {
      return {
        globalConfig: {
          module: ['supplier'],
          componentName: ['goods']
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          'startTime': undefined,
          'endTime': undefined,
          'onSale': undefined,
          'categoryId': undefined,
          'brandId': undefined,
          'supplierId': undefined,
          'retailPrice': undefined,
          'contrastPrice': undefined,
          'counterPrice': undefined,
          'introduction': undefined,
          'memo': undefined,
          'monthHits': undefined,
          'monthHitsDate': undefined,
          'monthSales': undefined,
          'monthSalesDate': undefined,
          'keywords': undefined,
          'gallery': undefined,
          'top': undefined,
          'goodsName': undefined
        },
        rules: {},
        form: {
          'onSale': undefined,
          'categoryId': undefined,
          'brandId': undefined,
          'supplierId': undefined,
          'retailPrice': undefined,
          'contrastPrice': undefined,
          'counterPrice': undefined,
          'introduction': undefined,
          'memo': undefined,
          'monthHits': undefined,
          'monthHitsDate': undefined,
          'monthSales': undefined,
          'monthSalesDate': undefined,
          'keywords': undefined,
          'gallery': undefined,
          'top': undefined,
          'goodsName': undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        goodsManager_btn_add: false,
        goodsManager_btn_upd: false,
        goodsManager_btn_del: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        casDataUrl: casObj,
        brandRemoteUrl: brandRemoteData,
        categoryCasUrl: categoryCasObj,
        key: '',
        formKey: ''
      }
    },
    computed: {
      ...mapGetters([
        'permissions'
      ])
    },
    filters: {},
    created() {
      this.getList()
      this.goodsManager_btn_add = this.permissions['goodsManager_btn_add']
      this.goodsManager_btn_upd = this.permissions['goodsManager_btn_upd']
      this.goodsManager_btn_del = this.permissions['goodsManager_btn_del']
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
            this.list = response.data.data.records
            this.refreshTable()
            this.total = response.data.data.total
            this.listLoading = false
          }
        )
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleDelete(row) {
        delObj(row.id)
          .then(response => {
              this.dialogFormVisible = false
              this.getList()
              globalNotify(this, response)
            }
          )
      },
      handleCreate() {
        console.log(this.$router)
        this.$router.push({ path: '/supplier/goods/add' })
      },
      refreshTable() {
        this.key = new Date().getTime()
      },
      refreshForm() {
        this.formKey = new Date().getTime()
      },
      handleUpdate(row) {
        this.$router.push({ path: '/supplier/goods/add', query: { goodsId: row.id } })
      },
      create(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
            if (valid) {
              addObj(this.form)
                .then((response) => {
                    this.dialogFormVisible = false
                    this.getList()
                    globalNotify(this, response)
                  }
                )
            } else {
              return false
            }
          }
        )
      },
      cancel(formName) {
        this.resetForm()
        this.dialogFormVisible = false
      },
      update(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
            if (valid) {
              this.dialogFormVisible = false
              this.form.password = undefined
              putObj(this.form).then((response) => {
                  this.dialogFormVisible = false
                  this.getList()
                  globalNotify(this, response)
                }
              )
            } else {
              return false
            }
          }
        )
      },
      resetForm() {
        this.form = {
          'onSale': undefined,
          'categoryId': undefined,
          'brandId': undefined,
          'supplierId': undefined,
          'retailPrice': undefined,
          'contrastPrice': undefined,
          'counterPrice': undefined,
          'introduction': undefined,
          'memo': undefined,
          'monthHits': undefined,
          'monthHitsDate': undefined,
          'monthSales': undefined,
          'monthSalesDate': undefined,
          'keywords': undefined,
          'gallery': undefined,
          'top': undefined,
          'goodsName': undefined
        }
      },
      handleSearch() {
        this.listQuery.page = 1
        this.getList()
      }
    }
  }
</script>
